/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.kamionowski.supreme.persistence.dao;

import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;
import org.kamionowski.supreme.persistence.entity.Host;
import org.kamionowski.supreme.persistence.entity.HostIP;
import org.kamionowski.supreme.persistence.entity.Host_;
import org.springframework.stereotype.Repository;

/**
 *
 * @author soldier
 */
@Repository
public class HostDAOJPA extends AbstractDAOJPA implements HostDAO {

    @Override
    public List<Host> getAllHosts() {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Host> query = builder.createQuery(Host.class);
        Root<Host> hostRoot = query.from(Host.class);
        query.select(hostRoot);
        query.orderBy(builder.desc(hostRoot.get(Host_.fullHostName)));
        return em.createQuery(query).getResultList();
    }

    @Override
    public Host getHost(String hostName) {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Host> query = builder.createQuery(Host.class);
        Root<Host> hostRoot = query.from(Host.class);
        query.select(hostRoot);
        return em.createQuery(query).getSingleResult();
    }

    @Override
    public List<HostIP> getIPs(String hostName) {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<HostIP> query = builder.createQuery(HostIP.class);
        Root<Host> hostRoot = query.from(Host.class);
        Join<Host,HostIP> ips = hostRoot.join( Host_.hostIPs );
        query.select(ips);
        return em.createQuery(query).getResultList();
    }


}
